package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.cache.BlackMobileCache;
import com.caiyi.financial.nirvana.sms.entity.SmsAccountBlacklist;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.enums.SubscribeEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsAccountBlacklistMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.QueryAccountBlacklistBean;
import com.github.pagehelper.PageHelper;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SmsAccountBlacklistService extends BaseService<SmsAccountBlacklist> {

    @Autowired
    private SmsAccountBlacklistMapper smsAccountBlacklistMapper;

    public int addAccountBlacklist(SmsAccountBlacklist smsAccountBlacklist) {
        YyAssert.isFalse(isExistAccountBlacklist(smsAccountBlacklist), ReturnCodeEnum.PARAMS_ERROR.code, "账号黑名单存在");
        int insertCount = this.insertByEntitySelective(smsAccountBlacklist);
        //redis中添加accountBlacklist
        String key = String.format(JedisKey.SMS_BLACKLIST_ACCOUNT, smsAccountBlacklist.getAccount());
        JedisService.sadd(key, smsAccountBlacklist.getPhone());
        Map map = new HashMap();
        map.put("method", SubscribeEnum.ADDMOBILE.method);
        map.put("phone", smsAccountBlacklist.getPhone());
        map.put("account", smsAccountBlacklist.getAccount());
        BlackMobileCache.addMobile(smsAccountBlacklist.getAccount(), smsAccountBlacklist.getPhone());
        JedisService.publish(JedisKey.SMS_SUBSCRIBE_API_ACTION, JSON.toJSONString(map));
        return insertCount;
    }

    public boolean isExistAccountBlacklist(SmsAccountBlacklist smsAccountBlacklist) {
        int count = smsAccountBlacklistMapper.selectAccountBlacklist(smsAccountBlacklist);
        return count > 0;
    }


    public int deleteAccountBlacklist(Integer id) {
        SmsAccountBlacklist smsAccountBlacklist = new SmsAccountBlacklist();
        smsAccountBlacklist.setId(id);
        smsAccountBlacklist = this.smsAccountBlacklistMapper.selectByPrimaryKey(smsAccountBlacklist);
        int deleteCount = this.deleteByPrimaryKey(smsAccountBlacklist);
        //redis中删除accountBlacklist
        String key = String.format(JedisKey.SMS_BLACKLIST_ACCOUNT, smsAccountBlacklist.getAccount());
        JedisService.srem(key, smsAccountBlacklist.getPhone());
        Map map = new HashMap();
        map.put("method", SubscribeEnum.DELMOBILE.method);
        map.put("phone", smsAccountBlacklist.getPhone());
        map.put("account", smsAccountBlacklist.getAccount());
        BlackMobileCache.delMobile(smsAccountBlacklist.getAccount(), smsAccountBlacklist.getPhone());
        JedisService.publish(JedisKey.SMS_SUBSCRIBE_API_ACTION, JSON.toJSONString(map));
        logger.info("deleteByAccountBlacklistId id {}", id);
        return deleteCount;
    }

    /**
     * 暂无使用
     *
     * @param smsAccountBlacklist
     * @return
     */
    public int updateAccountBlacklist(SmsAccountBlacklist smsAccountBlacklist) {
        //删除redis中原来数据
        SmsAccountBlacklist blacklist = new SmsAccountBlacklist();
        blacklist.setId(smsAccountBlacklist.getId());
        blacklist = this.selectByPrimaryKey(blacklist);
        String key = String.format(JedisKey.SMS_BLACKLIST_ACCOUNT, blacklist.getAccount());
        JedisService.srem(key, blacklist.getPhone());
        int updateCount = this.updateByEntitySelective(smsAccountBlacklist);
        //redis中更新blacklist
        key = String.format(JedisKey.SMS_BLACKLIST_ACCOUNT, blacklist.getAccount());
        JedisService.sadd(key, smsAccountBlacklist.getPhone());
        BlackMobileCache.addMobile(smsAccountBlacklist.getAccount(), smsAccountBlacklist.getPhone());
        logger.info("updateByAccountBlacklistId id {}", smsAccountBlacklist.getId());
        return updateCount;
    }

    public PageBean<SmsAccountBlacklist> getAccountBlacklistByParams(QueryAccountBlacklistBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsAccountBlacklist> list = smsAccountBlacklistMapper.selectAccountBlacklistListByParams(bean);
        PageBean<SmsAccountBlacklist> pageBean = new PageBean<>(list);
        return pageBean;
    }
}
